package com.jeeplus.modules.project.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.ProjectKnot;
import com.jeeplus.modules.project.service.BaseProjectInfoService;
import com.jeeplus.modules.project.service.ProjectKnotService;

/**
 * 项目结项申请Controller
 * @author jacky
 * @version 2018-01-26
 */
@Controller
@RequestMapping(value = "${adminPath}/project/projectKnot")
public class ProjectKnotController extends BaseController {
	@Autowired
	private BaseProjectInfoService baseProjectInfoService;
	@Autowired
	private ProjectKnotService projectKnotService;
	
	@ModelAttribute
	public ProjectKnot get(@RequestParam(required=false) String id) {
		ProjectKnot entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectKnotService.get(id);
		}
		if (entity == null){
			entity = new ProjectKnot();
		}
		return entity;
	}
	
	/**
	 * 进入主页面
	 * @param baseProjectInfo
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:projectKnot:index")
	@RequestMapping(value = "index")
	public String index(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		//baseProjectInfo.setStatus(6);//进行了中期检查的项目 才能结项
		List<Integer> list = new ArrayList<Integer>();
		list.add(6);
		list.add(7);
		list.add(8);
		baseProjectInfo.setStatusArray(list);
		baseProjectInfo.setFlag(0);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/knot/basePtInfoKnotList";
	}
	
	/**
	 * 项目结项申请列表页面
	 */
	@RequiresPermissions("project:projectKnot:list")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectKnot projectKnot, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectKnot> page = projectKnotService.findPage(new Page<ProjectKnot>(request, response), projectKnot); 
		model.addAttribute("page", page);
		return "modules/project/knot/projectKnotList";
	}

	/**
	 * 查看，增加，编辑项目结项申请表单页面
	 */
	@RequiresPermissions(value="project:projectKnot:edit")
	@RequestMapping(value = "form")
	public String form(ProjectKnot projectKnot, Model model) {
		if(projectKnot.getBasePtInfo() != null){
			if(!StringUtils.isEmpty(projectKnot.getBasePtInfo().getId())){
				BaseProjectInfo bptInfo = baseProjectInfoService.get(projectKnot.getBasePtInfo().getId());
				projectKnot.setBasePtInfo(bptInfo);
			}
		}
		
		String view = "projectKnotForm";
		// 查看审批申请单
		if (StringUtils.isNotBlank(projectKnot.getId())){
			// 环节编号
			String taskDefKey = projectKnot.getAct().getTaskDefKey();
			// 查看工单
			if(projectKnot.getAct().isFinishTask()){
				view = "projectKnotViewForm";
			}else{
				view = "projectKnotApprvForm";
			}
		}
		
		model.addAttribute("projectKnot", projectKnot);
		return "modules/project/knot/"+view;
	}

	/**
	 * 保存项目结项申请
	 */
	@RequiresPermissions(value="project:projectKnot:edit")
	@RequestMapping(value = "save")
	public String save(ProjectKnot projectKnot, Model model, RedirectAttributes redirectAttributes) throws Exception{
		//给一个结项申请时间
		projectKnot.setKnotDate(new Date());
		projectKnotService.save(projectKnot);//保存
		addMessage(redirectAttributes, "保存项目立项申请成功");
		return "redirect:" + adminPath + "/act/task/todo/";
	}
	
	/**
	 * 删除项目结项申请
	 */
	@RequiresPermissions("project:projectKnot:edit")
	@RequestMapping(value = "delete")
	public String delete(ProjectKnot projectKnot, RedirectAttributes redirectAttributes) {
		projectKnotService.delete(projectKnot);
		addMessage(redirectAttributes, "删除项目立项申请成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectKnot/?repage";
	}
	
	/**
	 * 批量删除项目结项申请
	 */
	@RequiresPermissions("project:projectKnot:edit")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			projectKnotService.delete(projectKnotService.get(id));
		}
		addMessage(redirectAttributes, "删除项目立项申请成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectKnot/?repage";
	}
	
	/**
	 * 工单执行（审核-完成任务）
	 * @param testAudit
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:projectKnot:edit")
	@RequestMapping(value = "saveAprov")
	public String saveAprov(ProjectKnot projectKnot, Model model) {
		projectKnotService.approveSave(projectKnot);
		return "redirect:" + adminPath + "/act/task/todo/";
	}
}